var t = getApp(), e = t.requirejs("core"), FoxUI = t.requirejs("foxui");

Page({
    data: {
      icons: t.requirejs("icons"),
      page: 1,
      loading: false,
      loaded: false,
      type: 0,
      list: [],
    },
    onLoad: function(e) {
      this.getList();
    },
    onShow: function() {
      this.setData({
        imgUrl: t.globalData.approot
      })
    },
    getList: function() {
      var t = this, type = t.data.type;
      t.setData({
          loading: true
      }), e.get("order/einvoice/getlist", {
          keywords: "",
          shanghuid: 0,
          checked: type,
          page: t.data.page,
      }, function(e) {
          var i = {
              loading: false,
              total: e.total,
              pagesize: e.pagesize,
              show: !0
          };
          e.list.length > 0 && (i.page = t.data.page + 1, i.list = t.data.list.concat(e.list), 
          e.list.length < e.pagesize && (i.loaded = true)), t.setData(i);
      });
    },
    onReachBottom: function() {
      this.data.loaded || this.data.list.length == this.data.total || this.getList();
    },
    tab: function(p) {
      var type = p.currentTarget.dataset.type;
      this.setData({
          list: [],
          page: 1,
          type: type,
          loaded: false,
      }), this.getList();
    },
    url: function(p) {
      var url = p.currentTarget.dataset.url;
      wx.navigateTo({ url: url, })
    },
    deletContract: function() {
      try {
        let file = wx.getFileSystemManager();
        file.readdir({
          dirPath: `${wx.env.USER_DATA_PATH}`,
          success: res => {
            if (res.files.length > 2) {
              file.unlink({
                filePath: `${wx.env.USER_DATA_PATH}/${res.files[0]}`,
                complete: res => {  }
              })
            }
          }
        })
      } catch (error) {  }
    },
    save: function(p) {
      var that = this, url = p.currentTarget.dataset.url;
      const fileExtName = ".pdf";
      const randfile = new Date().getTime() + fileExtName;
      const newPath = `${wx.env.USER_DATA_PATH}/${randfile}`;
      // wx.showLoading({
      //   title: "加载中..."
      // }), setTimeout(function() {
      //     wx.hideLoading();
      // }, 1e3)
      that.deletContract();
      wx.downloadFile({
        url: url,
        filePath: newPath,
        success: function (res) {
          const filePath = res.tempFilePath;
          wx.openDocument({
            filePath: newPath,
            showMenu: true,
            fileType: "pdf",
            success: function (res) {  }
          })
        },
        fail: function (res) {
          wx.hideLoading();
        }
      })
    },
    save2: function(p) {
      var t = this, file = p.currentTarget.dataset.url;
      wx.getSetting({
          success: function(e) {
              e.authSetting["scope.writePhotosAlbum"] ? (wx.showLoading({
                  title: "下载中..."
              }), setTimeout(function() {
                  wx.hideLoading();
              }, 1e3), wx.downloadFile({
                  url: file,
                  success: function(e) {
                      wx.saveImageToPhotosAlbum({
                          filePath: e.tempFilePath,
                          success: function(e) {
                            FoxUI.toast(t, "保存成功");
                          },
                          fail: function(e) {
                            FoxUI.toast(t, "保存失败");
                          }
                      });
                  }
              })) : wx.authorize({
                  scope: "scope.writePhotosAlbum",
                  fail: function() {
                      wx.showModal({
                          title: "警告",
                          content: "您点击了拒绝授权，将无法正常使用保存图片或视频的功能体验，请删除小程序重新进入。"
                      });
                  }
              });
          }
      });
    },
    save3: function(p) {
      var file = p.currentTarget.dataset.url;
      wx.setClipboardData({
        data: String(file),
        success: function (res) {
          wx.showToast({
            title: "复制成功,请至浏览器下载",
            icon: "none",
            duration: 3000,
          })
          // wx.getClipboardData({
          //   success: function (res) {
          //     wx.showToast({
          //       title: "复制成功,请至浏览器下载",
          //       icon: "none",
          //       duration: 3000,
          //     })
          //   },
          //   fail: function(res) {
          //     wx.showToast({
          //       title: "复制失败",
          //       icon: "none",
          //       duration: 2000,
          //     })
          //   }
          // })
        },
      })
    },
});